from sqlalchemy import Column, Integer, String, Text, ForeignKey, JSON
from sqlalchemy.orm import relationship
from .database import Base

class Workflow(Base):
    __tablename__ = "workflows"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False)
    description = Column(Text)
    user_id = Column(Integer, nullable=False)
    nodes = Column(JSON)
    edges = Column(JSON)

class Node(Base):
    __tablename__ = "nodes"

    id = Column(Integer, primary_key=True, index=True)
    workflow_id = Column(Integer, ForeignKey("workflows.id"))
    type = Column(String(50), nullable=False)
    label = Column(String(100))
    params = Column(JSON)
    prev_node_id = Column(Integer, ForeignKey("nodes.id"))
    next_node_id = Column(Integer, ForeignKey("nodes.id"))

    workflow = relationship("Workflow", back_populates="nodes")
    prev_node = relationship("Node", foreign_keys=[prev_node_id], remote_side=[id])
    next_node = relationship("Node", foreign_keys=[next_node_id], remote_side=[id])

Workflow.nodes = relationship("Node", back_populates="workflow") 